<?php
/**
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * Session Save Handler DB 存储
 *
 * @package    classes
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
class EGP_Session_SaveHandler_Db implements EGP_Session_SaveHandler_Interface
{
    /**
     * Db Adapter
     *
     * @var EGP_Db_Adapter_Abstract
     */
    protected $_db = null;

    /**
     * Database Name
     *
     * @var string
     */
    protected $_dbname = null;

    /**
     * constructor
     *
     * @param EGP_Db_Adapter_Abstract $db
     * @param string                  $dbname
     */
    public function __construct(EGP_Db_Adapter_Abstract $db, $dbname = null)
    {
        $this->_db = $db;
        $this->_dbname = $dbname;
    }

    /**
     * Open Session - retrieve resources
     *
     * @param  string $save_path
     * @param  string $name
     * @return boolean
     */
    public function open($save_path, $name)
    {
        return true;
    }

    /**
     * Close Session - free resources
     *
     * @return boolean
     */
    public function close()
    {
        return true;
    }

    /**
     * Read session data
     *
     * @param  string  $id
     * @return string
     */
    public function read($id)
    {
        return $this->_db->select()
                ->from('session', 'data', $this->_dbname)
                ->where('sid = ?', $id)
                ->fetchOne();
    }

    /**
     * Write Session - commit data to resource
     *
     * @param  string  $id
     * @param  mixed   $data
     * @return boolean
     * @throws EGP_Exception
     */
    public function write($id, $data)
    {
        if (empty($id)) return false;
        $set = array(
            'sid' => $id,
            'data' => $data,
            'modify_time' => time()
        );
        try {
            $this->_db->replace('session', $set, $this->_dbname);
            return true;
        } catch (EGP_Exception $e) {
            return false;
        }
    }

    /**
     * Destroy Session - remove data from resource for
     * given session id
     *
     * @param  string  $id
     * @return boolean
     * @throws EGP_Exception
     */
    public function destroy($id)
    {
        try {
            $where = $this->_db->quoteInto('sid = ?', $id);
            $this->_db->delete('session', $where, $this->_dbname);
            return true;
        } catch (EGP_Exception $e) {
            return false;
        }
    }

    /**
     * Garbage Collection - remove old session data older
     * than $maxlifetime (in seconds)
     *
     * @param  int  $maxlifetime
     * @return boolean
     * @throws EGP_Exception
     */
    public function gc($maxlifetime)
    {
        if ($maxlifetime <= 0) {
            $maxlifetime = 2592000; //30 days
        }
        try {
            $where = $this->_db->quoteInto('modify_time + ? < ?', $maxlifetime, time());
            $this->_db->delete('session', $where, $this->_dbname);
            return true;
        } catch (EGP_Exception $e) {
            return false;
        }
    }
}
